
#include "3dClube.h"
#include "MatrixConvert.h"
#include "TrigMath.h"

// object defines
#define NUM_OBJECTS     6       // number of objects on a row
#define OBJECT_SPACING  250     // spacing between objects


C3dClube::C3dClube()
{
	float init[] = {0,0,100,1};
	poly1_pos = init;

	init[0] = 0;
	init[1] = 0;
	init[2] = 0;
	init[3] = 1;
	cam_pos		= init;

	init[0] = 0;
	init[1] = 0;
	init[2] = 0;
	init[3] = 1;
	cam_dir		= init;

	init[0] = 5.0;
	init[1] = 5.0;
	init[2] = 5.0;
	init[3] = 1;
	vscale	= init;

	init[0] = 0;
	init[1] = 0;
	init[2] = 0;
	init[3] = 1;
	vpos	= init;

	init[0] = 0;
	init[1] = 0;
	init[2] = 0;
	init[3] = 1;
	vrot	= init;
}
	
C3dClube::~C3dClube()
{

}

int C3dClube::Game_Init()
{
	// this function is where you do all the initialization 
	// for your game

	int index; // looping var

	// start up DirectDraw (replace the parms as you desire)
	int nRet = m_ddraw.Init(m_hWnd, m_WndWidth, m_WndHeight, m_bpp, m_bFullScreen);
	//if (nRet != 0)
	//	return-1;

	m_graphGdi.Init(&m_ddraw);

	// initialize directinput
	m_gameInput.Init(m_hInstance, m_hWnd);

	// acquire the keyboard 
	m_gameInput.Init_Keyboard();


	// initialize directsound and directmusic
	m_gameSound.Init(m_hWnd);
	m_gameMusic.Init(m_hWnd, m_gameSound.GetDSound());

	// hide the mouse
	ShowCursor(FALSE);

	// seed random number generator
	srand(m_gameClock.Start_Clock());

	//
	CTrigMath::Build_Sin_Cos_Tables();


	// initialize the camera with 90 FOV, normalized coordinates
	CPoint4d target;
	cam.Init(
		CAM_MODEL_EULER, // euler camera model
		cam_pos,  // initial camera position
		cam_dir,  // initial camera angles
		target,      // no initial target
		50.0,      // near and far clipping planes
		500.0,
		90.0,      // field of view in degrees
		m_WndWidth,   // size of final screen viewport
		m_WndHeight);

	// load the cube
	m_plgFile.Init(_T("D:/BBus/CServer/modules/Lib3dModel/cube2.plg"));
	m_plgFile.LoadObject(obj, &vscale, &vpos, &vrot);

	// set the position of the cube in the world
	obj.world_pos.SetX(0);
	obj.world_pos.SetY(0);
	obj.world_pos.SetZ(100);

	// return success
	return 0;

} // end Game_Init


int	C3dClube::Game_Main()
{
	// this is the workhorse of your game it will be called
	// continuously in real-time this is like main() in C
	// all the calls for you game go here!

	static CMatrix44 mrot; // general rotation matrix
	static float x_ang = 0, y_ang = 2, z_ang = 0;

	int index; // looping var

	// start the timing clock
	m_gameClock.Start_Clock();

	// clear the drawing surface 
	m_ddraw.Fill_Surface(BACK_SURFACE, 0);

	// read keyboard and other devices here
	m_gameInput.Init_Keyboard();

	// game logic here...
	obj.Reset();

	// reset angles
	x_ang = z_ang = 0;

	// is user trying to rotate
	if (KEY_DOWN(VK_DOWN))
		x_ang = 1;
	else if (KEY_DOWN(VK_UP))
		x_ang = -1; 
	
	// lock the back buffer
	m_ddraw.Lock_Back_Surface();

	RECT clipRect = {0, 0, m_WndWidth, m_WndHeight};
	obj.Init(m_ddraw.GetBackBuffer(), m_ddraw.GetBackLpitch(), clipRect, &cam);

	// generate rotation matrix around y axis
	XYZ_Rotation_MATRIX4X4(x_ang, y_ang, z_ang, mrot);

	// rotate the local coords of single polygon in renderlist
	obj.Transform(mrot, TRANSFORM_LOCAL_ONLY, 1);

	// perform local/model to world transform
	obj.Model_To_World();

	// generate camera matrix
	cam.Build_Matrix_Euler(CAM_ROT_SEQ_ZYX);

	// remove backfaces
	obj.Remove_Backfaces();

	// apply world to camera transform
	obj.World_To_Camera();

	// apply camera to perspective transformation
	obj.Camera_To_Perspective();

	// apply screen transform
	obj.Perspective_To_Screen();

	// draw instructions
		// draw instructions
	m_graphGdi.Draw_Text("Press ESC to exit. Use UP ARROW and DOWN ARROW to rotate.", 0, 0, RGB(0,255,0));

	// render the polygon list
	obj.Draw_Wire16();

	// unlock the back buffer
	m_ddraw.Unlock_Back_Surface();

	// flip the surfaces
	m_ddraw.Flip(m_Window_client_x0, m_Window_client_y0);

	// sync to 30ish fps
	m_gameClock.Wait_Clock(30);

	// check of user is trying to exit
	if (KEY_DOWN(VK_ESCAPE) || m_gameInput.GetKeyState(DIK_ESCAPE))
	{
		PostMessage(m_hWnd, WM_DESTROY,0,0);

	} // end if

	return 0;
}

int	C3dClube::Game_Shutdown()
{
	// this function is where you shutdown your game and
	// release all resources that you allocated

	// shut everything down

	// release all your resources created for the game here....


	CGameMain::Game_Shutdown();

	return 0;
}
